﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace Orderr
{
    [Serializable]
    public class OrderService
    {
        List<Order> Orders;
        public OrderService()
        {
            Orders = new List<Order>();
        }
        public void add(Order order)
        {
            var query1 = from s in Orders
                         where s.oid == order.oid
                         select s;
            if (query1.Count() == 0) Orders.Add(order);
            else throw new ArgumentException("该订单已存在");
        }
        public void delete(int oid)
        {
            var query2 = from s in Orders
                         where s.oid == oid
                         select s;
            if (query2.Count() == 0) throw new ArgumentException("该订单不存在");
            else Orders.Remove(query2.FirstOrDefault());
        }
        public void edit(Order order)
        {
            var query1 = from s in Orders
                         where s.oid == order.oid
                         select s;
            if (query1.Count() == 0) throw new ArgumentException("该订单不存在");
            if (query1.FirstOrDefault().oid != order.oid) throw new ArgumentException("订单号不能改变");
            delete(order.oid);
            add(order);
        }
        public String QueryByOid(int oid)
        {
            var query2 = from s in Orders
                         where s.oid == oid
                         select s;
            if (query2.Count() == 0) throw new ArgumentException("该订单不存在");
            return "OID为" + oid + "订单为：\n" + query2.FirstOrDefault().ToString();
        }
        public String QueryByGood(Good good)
        {
            var query2 = from s in Orders
                         where
                            (from x in s.Details
                             where x.good == good
                             select x).Count() > 0
                         orderby s.totalPrice
                         select s;
            if (query2.Count() == 0) throw new ArgumentException("该订单不存在");
            String ret = "包含商品" + good + "的有\n";
            foreach (Order x in query2)
            {
                ret += (x.ToString());
            }
            return ret;
        }
        public String QueryByPrice(int price)
        {
            var query2 = from s in Orders
                         where s.totalPrice >= price
                         select s;
            if (query2.Count() == 0) throw new ArgumentException("该订单不存在");
            String ret = "订单金额大于等于" + price + "的有\n";
            foreach (Order x in query2)
            {
                ret += (x.ToString());
            }
            return ret;
        }
        public void export()
        {
            XmlSerializer turn = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("output.xml", FileMode.Create))
            {
                turn.Serialize(fs, Orders);
            }
        }
        public void inport()
        {
            XmlSerializer turn = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("input.xml", FileMode.Open))
            {
                List<Order> Orderss = (List<Order>)turn.Deserialize(fs);
                Orders = Orderss;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Good good1 = new Good("banana", 848);
            Good good2 = new Good("apple", 666);
            Good good3 = new Good("milk", 55);
            Client client1 = new Client("XB");
            Client client2 = new Client("TLM");
            Client client3 = new Client("ZRZ");
            Order list1 = new Order(233, client1, new List<OrderDetails>());
            list1.Details.Add(new OrderDetails(good1, 3));
            list1.Details.Add(new OrderDetails(good2, 4));
            Order list2 = new Order(234, client2, new List<OrderDetails>());
            list2.Details.Add(new OrderDetails(good1, 3));
            list2.Details.Add(new OrderDetails(good3, 5));
            Order list3 = new Order(235, client3, new List<OrderDetails>());
            list3.Details.Add(new OrderDetails(good1, 2));
            list3.Details.Add(new OrderDetails(good3, 4));
            list3.Details.Add(new OrderDetails(good2, 5));

            OrderService master = new OrderService();
            master.add(list1);
            master.add(list2);
            master.add(list3);

            try { Console.WriteLine(master.QueryByOid(233)); }
            catch (ArgumentException e) { Console.WriteLine(e); }
            try { Console.WriteLine(master.QueryByPrice(2819)); }
            catch (ArgumentException e) { Console.WriteLine(e); }
            try { Console.WriteLine(master.QueryByGood(good1)); }
            catch (ArgumentException e) { Console.WriteLine(e); }

            master.export();
        }
    }
}
